//
//  LineTroopForm.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/9.
//
//

#include "LineTroopForm.hpp"

//确定队伍占用的空间大小
#define MONSTER_SCALE_FACTOR 1.2
#define GROUP_SCALE_FACTOR 1.5

void LineTroopForm::group()
{
    groupInternal(*members);
}
void LineTroopForm::groupInternal(const Vector<Node *> &members)
{
    //确定队伍的活动空间大小
    auto globalVisibleSize = Director::getInstance()->getVisibleSize();
    auto troopVisibleWidth = 0.0f, troopVisibleHeight = 0.0f;
    troopVisibleWidth = globalVisibleSize.width;
    troopVisibleHeight = globalVisibleSize.height;
    
    float width = 0.0f, height = 0.0f;
    float minWidth = 100000000.0f;
    for(auto it = members.begin(); it != members.end(); it++)
    {
        auto size = (*it)->getContentSize();
        width += size.width;
        if(size.width < minWidth)
            minWidth = size.width;
        if(size.height > height)
            height = size.height;
    }
    
    auto gap = (troopVisibleWidth - width) / (members.size() - 1);
    gap = MIN(gap, minWidth);
    gap = MAX(gap, 0);
    
    troopSize.width = width + (members.size() - 1) * gap;
    troopSize.height = height;
    
    troopVisibleSize.height = troopVisibleHeight;
    troopVisibleSize.width = MAX(troopVisibleWidth, troopSize.width);
    
    //将队伍中的怪物按加入顺序进行水平线性排序
    bool isOddNum = (members.size() % 2);  //怪物个数是否是奇数
    auto leftEndIndex = -1, rightBeginIndex = -1;
    auto position = Vec2(0.0f, troopVisibleHeight + troopSize.height /  2 + 10);
    if(isOddNum)
    {
        leftEndIndex = members.size() / 2;
        rightBeginIndex = members.size()  / 2;
        
        auto monster = members.at(leftEndIndex);
        position.x = globalVisibleSize.width / 2;
        monster->setPosition(position);
    }
    else
    {
        rightBeginIndex = members.size() / 2;
        
        auto monster = members.at(rightBeginIndex);
        position.x = (globalVisibleSize.width / 2 +
                      (monster->getAnchorPoint().x * monster->getContentSize().width) + gap / 2);
        monster->setPosition(position);
        
        leftEndIndex = rightBeginIndex - 1;
        monster = members.at(leftEndIndex);
        position.x = (globalVisibleSize.width / 2 -
                      ((1 - monster->getAnchorPoint().x) * monster->getContentSize().width) - gap / 2);
        monster->setPosition(position);
    }
    for(int i = leftEndIndex - 1; i >= 0; i--)
    {
        auto current = members.at(i);
        auto right = members.at(i + 1);
        
        position.x = (right->getPosition()).x -
            (right->getAnchorPoint().x * right->getContentSize().width) -
            ((1 - current->getAnchorPoint().x) * current->getContentSize().width) - gap;
        
        current->setPosition(position);
    }
    for(int i = rightBeginIndex + 1; i < members.size(); i++)
    {
        auto current = members.at(i);
        auto left = members.at(i - 1);
        
        position.x = (left->getPosition()).x +
            ((1 - left->getAnchorPoint().x) * left->getContentSize().width) +
            (current->getAnchorPoint().x * current->getContentSize().width) + gap;
        
        current->setPosition(position);
    }
    //排列完毕
}

void LineTroopForm::train()
{
    getAction()->train(*members);

}
std::vector<TroopStyleType> LineTroopForm::allowedTroopStyles()
{
    std::vector<TroopStyleType> styles;
    
    styles.push_back(TYPE_LINEONETYPESTYLE);
    styles.push_back(TYPE_LINETWOSTYLESSTYLE);
    
    return styles;
}
std::vector<TroopActionStyle> LineTroopForm::allowedTroopActionStyle()
{
    std::vector<TroopActionStyle> types;
    types.push_back(TYPE_TRANSLATIONASGROUP);
    if(getNumOfMonster() % 2)
        types.push_back(TYPE_ODDMOVEEVENSTILL);
    return types;
}




